package my2Cents;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.Random;

import my2Cents.properties.KeyAssign;
import net.phys2d.math.Vector2f;
import net.phys2d.raw.Body;
import net.phys2d.raw.BodyList;
import net.phys2d.raw.shapes.PhysShape;

public class Thruster extends Body {
	private float power = 0;
	private int keyForward = -1;
	private int keyReverse = -1;
	private int keyOff = -1;
	private boolean toggled = false;
	private int p = 0;
	
	public Thruster(PhysShape shape, float m, float power, boolean isStatic) {
		super(shape, m);
		if(isStatic) {
			this.setMoveable(false);
		}
		this.power = power;
		shape.setName("Thruster");
	}
	
	public void setReverseCode(int c) {
		keyReverse = c;
	}
	
	public void setForwardCode(int c) {
		keyForward = c;
	}
	
	public void setOffCode(int off) {
		keyOff = off;
	}
	
	public void think() {
		super.think();
		
		if(!toggled) p=0;
		if(Sandbox.keyIsDown(keyForward))
			p = 1;
		
		if(Sandbox.keyIsDown(keyReverse))
			p = -1;
		
		if(Sandbox.keyIsDown(keyOff) && toggled) {
			p = 0;
		}
		
		float r = getRotation();
		double dx = Math.cos(r + 180/57.3);
		double dy = Math.sin(r + 180/57.3);
		this.setForce(
				(float) (dx*power*p),
				(float) (dy*power*p));
	}
	
	protected void drawCenter(Graphics2D g, Vector2f pos, float rot) {
		int x = (int)pos.x;
		int y = (int)pos.y;
		
		for(int i=0; i<4; i++) {
			rot += (new Random().nextFloat()-0.5f)*0.3f;
			
			double dx = Math.cos(rot + 180/57.3);
			double dy = Math.sin(rot + 180/57.3);
			
			double d = -p;
			d = d * (new Random().nextFloat()+0.5f)*30;
			
			int x2 = (int) (x + dx*d);
			int y2 = (int) (y + dy*d);
			
			g.setColor(new Color(20,20,20));
			g.drawLine(x, y, x2, y2);
			
			g.setColor(new Color(255,100,70));
			g.drawLine(x, y, x2, y2);
		}
	}
	
	public void draw(Graphics2D g) {
		PhysShape s = getShape();
		Vector2f pos = (Vector2f) this.getPosition();
		float rot = this.getRotation();
		
		g.setColor(getFillColor());
		g.fill(s.getGeneralPath(pos,rot));
		
		g.setColor(getDrawColor());
		s.draw(g,pos,rot);
		drawCenter(g,pos,rot);
		
		drawShell(g,pos,rot,s);
		

		int x = (int)pos.x;
		int y = (int)pos.y;
		double dx1 = Math.cos(rot + 200/57.3);
		double dy1 = Math.sin(rot + 200/57.3);
		
		double dx2 = Math.cos(rot + 160/57.3);
		double dy2 = Math.sin(rot + 160/57.3);
		
		double d = -p;
		if(d == 0) d = -1;
		d = d * 20;
		
		int x2 = (int) (x + dx1*d);
		int y2 = (int) (y + dy1*d);
		
		int x3 = (int) (x + dx2*d);
		int y3 = (int) (y + dy2*d);
		
		g.setColor(new Color(0,0,0));
		g.drawLine(x, y, x2, y2);
		g.drawLine(x, y, x3, y3);
	}

	public void setToggled(boolean toggled) {
		this.toggled = toggled;
	}
	
	public Body duplicate() {
		Thruster nb = new Thruster(this.getShape(),this.getMass(),power,this.isStatic());
		nb.keyForward = this.keyForward;
		nb.keyOff = this.keyOff;
		nb.keyReverse = this.keyReverse;
		nb.p = this.p;
		nb.toggled = this.toggled;
		nb.power = this.power;
		nb.setPosition(
				this.getPosition().getX(), 
				this.getPosition().getY());
		nb.setRotation(this.getRotation());
		nb.setIdentity(this.getIdentity());
		return nb;
	}

}
